package xhwl.insurance.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import xhwl.insurance.R;
import xhwl.insurance.http.bean.resp.SaleInfoResp;
import xhwl.insurance.http.bean.resp.SalesCardData;
import xhwl.insurance.ui.SaleCardDetailActivity;

/**
 * 售卡页面右侧详细的adapter
 */

public class SaleCardDetailListAdapter extends BaseAdapter {
    Context context;
    List<SalesCardData.DataBeanX.DataBean> list;

    public SaleCardDetailListAdapter(Context context, List<SalesCardData.DataBeanX.DataBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.item_detaillist, null);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (list.get(position).getGroupName() != null) {
            holder.name_card.setVisibility(View.VISIBLE);
            holder.name_card.setText(list.get(position).getGroupName());
        } else {
            holder.name_card.setVisibility(View.GONE);
        }
        String name_title = list.get(position).getCARD_NAME() != null ? list.get(position).getCARD_NAME() : "";
        holder.name_title.setText(name_title);
        Glide.with(context)
                .load(list.get(position).getCARD_PIC())
                .error(R.drawable.card_heart)
                .into(holder.icon_card);
        convertView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, SaleCardDetailActivity.class);
                intent.putExtra("jumpurl", list.get(position).getSKIP_TO());
                intent.putExtra("cardpic", list.get(position).getCARD_PIC());
                intent.putExtra("cardprice", list.get(position).getCARD_PRICE());
                intent.putExtra("saletype", list.get(position).getSALE_TYPE());
                intent.putExtra("yw_id", list.get(position).getYW_ID());
                intent.putExtra("card_name", list.get(position).getCARD_NAME());
                intent.putExtra("c_type", list.get(position).getC_TYPE());
                context.startActivity(intent);
            }
        });
        return convertView;
    }

    public static class ViewHolder {
        public View rootView;
        public TextView name_card;
        public ImageView icon_card;
        public TextView name_title;

        public ViewHolder(View rootView) {
            this.rootView = rootView;
            this.name_card = (TextView) rootView.findViewById(R.id.name_card);
            this.icon_card = (ImageView) rootView.findViewById(R.id.icon_card);
            this.name_title = (TextView) rootView.findViewById(R.id.name_title);
        }
    }
}
